package com.example.broadcast;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.broadcast.receiver.FirstOrderReceiver;
import com.example.broadcast.receiver.SecondOrderReceiver;
import com.example.broadcast.receiver.StandardReceiver;
import com.example.broadcast.receiver.StaticRegisteredReceiver;
import com.example.broadcast.receiver.TimeTickReceiver;

public class MainActivity extends AppCompatActivity {

    private StandardReceiver standardReceiver;
    private FirstOrderReceiver firstOrderReceiver;

    /**
     * 表明顺序广播的 Action
     */
    private String OrderedBroadcastAction;
    private SecondOrderReceiver secondOrderReceiver;
    private TimeTickReceiver timeTickReceiver;
    private TextView timeTickView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        OrderedBroadcastAction = getPackageName() + ".Ordered";


        findViewById(R.id.sendByBroadcast).setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setAction(getString(R.string.testBroadcast));
            intent.setType("text/plain");
            intent.putExtra("data", "发送一个友好的问候数据");
            sendBroadcast(intent);
        });

        findViewById(R.id.sendOrderedBroadcast).setOnClickListener(v -> {

            Intent intent = new Intent(OrderedBroadcastAction);
            intent.putExtra("msg", "这是一条顺序广播");
            // 注意发送有序广播的方法是 sendOrderedBroadcast
            sendOrderedBroadcast(intent,null);

        });

        findViewById(R.id.sendOrderedBroadcastInterruptedBySecond).setOnClickListener(v->{
            Intent intent = new Intent(OrderedBroadcastAction);
            intent.putExtra("msg", "这是一条顺序广播");
            intent.putExtra("processOnlyOnce", "只处理一次");
            // 注意发送有序广播的方法是 sendOrderedBroadcast
            sendOrderedBroadcast(intent,null);
        });

        findViewById(R.id.sendToStaticRegisteredReceiver).setOnClickListener(v->{

            // 可以填入 Action ， Receiver 在收到的时候可以进行路由匹配
            Intent intent = new Intent();

            intent.setComponent(
                    new ComponentName(this,"com.example.broadcast.receiver.StaticRegisteredReceiver")
            );
            sendBroadcast(intent,getString(R.string.PermissionStaticRegisteredBroadcastReceiver));
        });

        timeTickView = findViewById(R.id.timeTickView);
    }

    @Override
    protected void onStart() {
        super.onStart();

        standardReceiver = new StandardReceiver(this);


        // 注册全局广播接收器
        IntentFilter intentFilter = null;
        try {
            intentFilter = new IntentFilter(getString(R.string.testBroadcast), "text/plain");
        } catch (IntentFilter.MalformedMimeTypeException e) {
            throw new RuntimeException(e);
        }

        registerReceiver(standardReceiver, intentFilter);

        /* 注意两个 IntentFilter 监听的是同一个Action

            多个 有序广播接收器 的 顺序处理规则
            1、优先级越大，越早收到
            2、优先级相同时，越早注册的接收器越早收到广播
         */

        // 注册顺序广播A
        firstOrderReceiver = new FirstOrderReceiver();

        IntentFilter firstIntentFilter = new IntentFilter(OrderedBroadcastAction);
        firstIntentFilter.setPriority(1);
        registerReceiver(firstOrderReceiver, firstIntentFilter);


        // 注册顺序广播B
        secondOrderReceiver = new SecondOrderReceiver();

        IntentFilter secondIntentFilter = new IntentFilter(OrderedBroadcastAction);
        secondIntentFilter.setPriority(10);
        registerReceiver(secondOrderReceiver, secondIntentFilter);


        // 接收系统时间广播
        IntentFilter timeIntentFilter = new IntentFilter(Intent.ACTION_TIME_TICK);

        timeTickReceiver = new TimeTickReceiver(timeTickView::setText);
        registerReceiver(timeTickReceiver,timeIntentFilter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(standardReceiver);
        unregisterReceiver(firstOrderReceiver);
        unregisterReceiver(secondOrderReceiver);
        unregisterReceiver(timeTickReceiver);
    }
}